/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command
{
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.display.StageDisplayState;
	import flash.filesystem.File;
	
	import multimedia.control.event.InitializeApplicationEvent;
	import multimedia.model.ModelLocator;
	import multimedia.util.LogUtil;
	
	import mx.binding.utils.BindingUtils;
	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEventLevel;
	import mx.logging.targets.FileTarget;
	import mx.logging.targets.TraceTarget;

	/**
	 * 	InitializeApplicationCommand
	 *
	 *	@author jemden
	 *	@version 1.0
	 */
	public class InitializeApplicationCommand extends SequenceCommand
	{
		private var model:ModelLocator;

		private var e:InitializeApplicationEvent;
		
		private static const LOG:ILogger = LogUtil.getLogger(InitializeApplicationCommand);

		override public function execute(event:CairngormEvent):void
		{
			this.model = ModelLocator.getInstance();
			this.e = event as InitializeApplicationEvent;
			this.nextEvent = e.nextEvent;

			//LOGGING
			var traceTarget:TraceTarget = new TraceTarget();
			traceTarget.filters = ["multimedia.*"];
			traceTarget.level = LogEventLevel.ALL;

			traceTarget.includeDate = true;
			traceTarget.includeTime = true;
			traceTarget.includeCategory = true;
			traceTarget.includeLevel = true;

			var fileTarget:FileTarget = new FileTarget(File.applicationStorageDirectory.resolvePath("Multimedia.log"));
			fileTarget.level = LogEventLevel.ALL;
			fileTarget.filters = ["multimedia.*"];
			fileTarget.includeDate = true;
			fileTarget.includeTime = true;
			fileTarget.includeCategory = true;
			fileTarget.includeLevel = true;

			//binding trace logging
			BindingUtils.bindSetter(function traceLogging(value:Boolean):void
			{
				if(value)
				{
					Log.addTarget(traceTarget);
					LOG.info("<<< Enabling trace logging >>>");
				} else
				{
					LOG.info("<<< Disabling trace logging >>>");
					Log.removeTarget(traceTarget);
				}
			}, model, "traceLogging");
			//binding fileLogging
			BindingUtils.bindSetter(function fileLogging(value:Boolean):void
			{
				if(value)
				{
					Log.addTarget(fileTarget);
					LOG.info("<<< Enabling file logging >>>");
				} else
				{
					LOG.info("<<< Disabling file logging >>>");
					Log.removeTarget(fileTarget);
				}
			}, model, "fileLogging");
			
			BindingUtils.bindSetter(function logLevel(value:Number):void
			{
				LOG.info("<<< Setting new Log level to {0}", value);
				traceTarget.level = value;
				fileTarget.level = value;
			}, model, "logLevel");


			if (model.fullscreenOnStartup)
			{
				model.mainState = "presentation";
				FlexGlobals.topLevelApplication.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
			}
			else
			{
				model.mainState = "config";
			}

			executeNextCommand();
		}
	}
}
